class Solution {
    public int minOperations(int[] nums, int x) {
        int left = 0;
        int right = nums.length - 1;
        int ret = Integer.MAX_VALUE;
        int tmp = x;
        int countLeft = 0;
        while(left < right && tmp >= 0) {
            int countRight = 0;
            while(tmp > 0) {
                tmp -= nums[right--];
                countRight++;
            }
            if(tmp == 0){
                ret = Math.min(ret, countLeft + countRight);
            }
            x -= nums[left++];
            countLeft++;
            tmp = x;
            right = nums.length - 1;
        }
        if(ret == Integer.MAX_VALUE) {
            return -1;
        }
        return ret;
    }
}